﻿using UnityEngine;

public class MoveState : BaseState<PlayerCtrl> 
{
	public Rigidbody2D Rigidbody2D;
	public Animator Animator;

	private Vector2 input;

	public override void Init(PlayerCtrl ctrl) {
		Data = new StateData("MoveState");

		Priority = 1;

		base.Init(ctrl);
	}

	public override void OnEnter(object args = null) {
		base.OnEnter(args);
		Rigidbody2D = owner.Rigidbody2D;
		Animator = owner.Animator;
	}

	public override void OnStateUpdate(object args = null)
	{
		if (owner.Enable)
		{
			base.OnStateUpdate(args);
			input = owner.input;
			if (input.magnitude > 0.01f)
			{
				if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
				{
					if (input.x > 0)
					{
						Animator.SetTrigger("MoveRight");
					}
					else
					{
						Animator.SetTrigger("MoveLeft");
					}
				}
				else
				{
					if (input.y > 0)
					{
						Animator.SetTrigger("MoveUp");
					}
					else
					{
						Animator.SetTrigger("MoveDown");
					}
				}
			}
		}
	}

	public override void OnStateFixedUpdate(object args = null)
	{
		if (owner.Enable)
		{
			base.OnStateFixedUpdate();
			Rigidbody2D.MovePosition(Rigidbody2D.position + input * owner.MoveSpeed * Time.fixedDeltaTime);
		}
	}

	public override void OnExit(object args = null) {
		base.OnExit(args);
	}

	void CheckDir()
	{
		
	}

	public override bool IsComplete() {
		return false;
	}
}
